package com.os.config;

import com.os.constant.Mq;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.net.InetAddress;

/**
 * @author 风亦未止
 * @date 2023/5/6 15:22
 */
@Configuration
public class MqConfig {
    @Autowired
    private ConfigInit configInit;

    /**
     * 序列化
     *
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public MessagePostProcessor  messagePostProcessor(){
        return message -> {
            //消息在队列中存活5秒
            message.getMessageProperties().setExpiration(String.valueOf(5000));
            return message;
        };
    }

    @Bean
    public MessageProperties messageProperties(){
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setExpiration(String.valueOf(2000));
        return messageProperties;
    }

    public String queueName(){
        return configInit.getQueueName();
    }

    public String tempQueue(){
        return Mq.TEMP_QUEUE;
    }

}
